package coroutines;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SampleCode {


    ExecutorService executors = Executors.newFixedThreadPool(10);

    public void changeProductImage() {
        for (int i = 0; i < 10000; i++) {
            int productId = i;
            executors.submit(new Runnable() {

                @Override
                public void run() {
                    Product p = retriveProduct(productId);
                    String updateImageUrl = updateImage(p.imageUrl);
                    p.imageUrl = updateImageUrl;
                    updateProduct(p);
                }

                private void updateProduct(Product p) {
                    // IO操作，耗时200毫秒
                }

                private String updateImage(String imageUrl) {
                    // 图片压缩算法，耗时500毫秒
                    return null;
                }

                private Product retriveProduct(int productId) {
                    // // IO操作，耗时100毫秒
                    return null;
                }
            });
        }
    }


    class Product {
        String imageUrl;
    }
}
